<template>
	<div id="app">
		<el-container class="content">
			<!-- 头部区域 -->
			<el-header>
				<div>
					<img src="../assets/log.png" alt="">
					<span>电商后台管理系统</span>
				</div>
				<el-button type="info" @click="logout">退出</el-button>
			</el-header>
			<!-- 页面主体区域 -->
			<el-container>
				<!-- 左侧边栏 -->
				<el-aside :width="isCollapse?'64px':'200px'">
					<div class="toggle-button" @click="toggle">|||</div>
					<el-menu background-color="#373d41" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
						<!-- 一级菜单 -->
						<el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
							<template slot="title">
								<i :class="iconObj[item.id]"></i>
								<span>{{item.authName}}</span>
							</template>
							<!-- 二级菜单 -->
							<el-menu-item :index="'/'+submenu.path" v-for="submenu in item.children" :key="submenu.id" @click="saveNavState('/'+submenu.path)">
								<template slot="title">
									<i class="el-icon-menu"></i>
									<span>{{submenu.authName}}</span>
								</template>
							</el-menu-item>
						</el-submenu>

					</el-menu>
				</el-aside>
				<!-- 右侧内容区域 -->
				<el-main>
					<router-view></router-view>	
				</el-main>
			</el-container>
		</el-container>
	</div>
</template>	

<script>
	export default{
		data(){
			return {
				menuList:[],
				// 被激活的链接地址
				activePath:'',
				isCollapse:false,
				iconObj:{
					125:'icon iconfont icon-yonghu',
					103:'icon iconfont icon-quanxian',
					101:'icon iconfont icon-shangpin',
					102:'icon iconfont icon-dingdan',
					145:'icon iconfont icon-iconfontpaixingbang'
				},
				
			}
		},
		created(){
			this.getMenuList();
			this.activePath=window.sessionStorage.getItem('activePath');

		},
		methods:{
			// 保存链接激活状态
			saveNavState(activePath){
				window.sessionStorage.setItem('activePath',activePath);
				this.activePath=activePath;
			},
			toggle(){
				this.isCollapse=!this.isCollapse;
			},
			logout(){
				window.sessionStorage.clear('token');
				this.$router.push('/login');
			},
			async getMenuList(){
				const {data:res}=await this.$http.get('menus')
				// console.log(res)
				if(res.meta.status!==200) return console.log('获取数据失败')
				this.menuList=res.data;
			}
		}
	}
</script>

<style scoped>
.el-header{
	background-color: #373d41;
	display: flex;
	justify-content: space-between;
	align-items: center;
	color: #fff;
	padding-left: 0;
	font-size: 20px;
}
.el-header div{
	display: flex;
	align-items: center;

}
.el-header div img{
	width: 150px;
	height: 40px;
}

.el-aside{
	background-color: rgb(52,58,69);
}
.el-aside .el-menu{
	border-right:none;
}

.el-main{
	background-color: rgb(230,234,236);
}

.content{
	height: 100%;
}
.iconfont{
	margin-right:10px;
}

.toggle-button{
	background-color: #4a5064;
	line-height: 24px;
	font-size: 10px;
	color:#fff;
	text-align: center;
	letter-spacing: 0.2em;
	cursor: pointer;
}
</style>